//
//  ACNetworking+HUD.m
//  ACFramework
//
//  Created by caoyq on 2019/1/15.
//

#import "ACNetworking+HUD.h"
#import "ACNetworkBody.h"
#import "ACProgressHUD.h"
#import "NSObject+ACExt.h"

@implementation ACNetworking (HUD)

//必定显示HUD转圈，可以指定hud显示的view，默认会找到栈顶控制器
+ (NSURLSessionTask *)postWithHUD: (UIView *)hudView
                          hudText: (NSString *)hudText
                        showError: (BOOL)showError
                              url: (NSString *)url
                           vcName: (NSString *)vcName
                  allHeaderFields: (NSDictionary *)allHeaderFields
                       parameters: (NSDictionary *)parameters
                          success: (ACResponseSuccessBlock)success
                          failure: (ACResponseFailureBlock)failure
{
    [ACProgressHUD showHUDWithText:hudText ToView:hudView];
    return [self postWithUrl:url vcName:vcName allHeaderFields:allHeaderFields parameters:parameters success:^(id  _Nullable responseObject) {
        [ACProgressHUD hideHUDFromView:hudView];
        !success ?  : success(responseObject);
    } failure:^(NSError * _Nullable error) {
        [ACProgressHUD hideHUDFromView:hudView];
        !failure ?  : failure(error);
        if (showError) {
            NSString *tip = error.userInfo[@"message"];
            if (tip.length > 0) {
                [ACProgressHUD showText:tip toView:hudView];
            }
        }
    }];
}

@end
